import requests
import unittest


class TPShopLogin(unittest.TestCase):
    def setUp(self):
        self.session = requests.Session()
        self.verify = 'http://192.168.0.100/index.php?m=Home&c=User&a=verify'
        self.login_url = 'http://192.168.0.100/index.php?m=Home&c=User&a=do_login'

    def tearDown(self):
        self.session.close()

    def test01_login_succeed(self):
        response = self.session.get(url=self.verify)
        self.assertEqual(200, response.status_code)
        self.assertIn('image', response.headers.get('Content-Type'))
        login_data = {"username": "13141415177", "password": "123456", "verify_code": "8888"}
        response = self.session.post(url=self.login_url, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get('status'))
        self.assertEqual('登陆成功', response.json().get('msg'))

    def test02_user_is_not_exit(self):
        response = self.session.get(url=self.verify)
        self.assertEqual(200, response.status_code)
        self.assertIn('image', response.headers.get('Content-Type'))
        login_data = {"username": "13141415197", "password": "123456", "verify_code": "8888"}
        response = self.session.post(url=self.login_url, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertIn('不存在', response.json().get('msg'))
        self.assertEqual(-1, response.json().get('status'))

    def test03_password_error(self):
        response = self.session.get(url=self.verify)
        self.assertEqual(200, response.status_code)
        self.assertIn('image', response.headers.get('Content-Type'))
        login_data = {"username": "13141415177", "password": "123956", "verify_code": "8888"}
        response = self.session.post(url=self.login_url, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-2, response.json().get('status'))
        self.assertIn('错误', response.json().get('msg'))
